/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * OrderView.java
 *
 * Created on Apr 1, 2011, 8:45:34 PM
 */

package views;

import common.Session;
import controllers.OrderController;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.table.DefaultTableModel;
import models.Customer;
import models.Order;
import models.OrderItem;
import models.Product;

/**
 *
 * @author pelle
 */
public class OrderEditView extends AbstractView {

    private OrderController orderCtr = null;
    private ArrayList<OrderItem> items = null;
    private ArrayList<Product> searchProducts = null;

    private boolean orderIsBeingEdited = false;

    /** Creates new form OrderView */
    public OrderEditView() {
        initComponents();
        
        orderIsBeingEdited = false;

        createLabel.setVisible(true);
        editLabel.setVisible(false);

        this.orderCtr = new OrderController();
        this.orderCtr.setEmployee(Session.employee);
    }

    public OrderEditView(int orderId) {
        initComponents();

        orderIsBeingEdited = true;

        createLabel.setVisible(false);
        editLabel.setVisible(true);

        this.orderCtr = new OrderController(orderId);
        putOutValues();
    }

    private void putOutValues() {
        this.updateDetails();
        this.insertCustomerInformations();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        itemsTable = new javax.swing.JTable();
        minusCount = new javax.swing.JButton();
        plusCount = new javax.swing.JButton();
        removeItem = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        searchField = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        productList = new javax.swing.JList();
        addItem = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        searchBtn = new javax.swing.JButton();
        createLabel = new javax.swing.JLabel();
        saveBtn = new javax.swing.JButton();
        editLabel = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        selectCustomer = new javax.swing.JButton();
        cusAddress = new javax.swing.JLabel();
        cusZipcode = new javax.swing.JLabel();
        cusName = new javax.swing.JLabel();
        cusPhone = new javax.swing.JLabel();
        cusCity = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        moms = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        subTotal = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        jLabel23 = new javax.swing.JLabel();
        total = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        amountLabel = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        itemsTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Antal", "Titel", "Á", "Subtotal"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        itemsTable.setMaximumSize(new java.awt.Dimension(300, 64));
        jScrollPane1.setViewportView(itemsTable);

        minusCount.setText("-");
        minusCount.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                minusCountActionPerformed(evt);
            }
        });

        plusCount.setText("+");
        plusCount.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                plusCountActionPerformed(evt);
            }
        });

        removeItem.setText("Fjern");
        removeItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeItemActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Tilføj vare til ordre"));

        searchField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                searchFieldKeyReleased(evt);
            }
        });

        productList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(productList);

        addItem.setText("Tilføj vare");
        addItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addItemActionPerformed(evt);
            }
        });

        jLabel1.setText("Søg:");

        searchBtn.setText("Søg");
        searchBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchBtnActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(addItem)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 469, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel1Layout.createSequentialGroup()
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(searchField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(searchBtn)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(searchField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(searchBtn))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 105, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(addItem)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        createLabel.setFont(new java.awt.Font("Tahoma", 1, 24));
        createLabel.setText("Opret");

        saveBtn.setText("» Gem ordre");
        saveBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveBtnActionPerformed(evt);
            }
        });

        editLabel.setFont(new java.awt.Font("Tahoma", 1, 24));
        editLabel.setText("Ret");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel4.setText("ordre");

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Kunde informationer"));

        selectCustomer.setText("Vælg kunde");
        selectCustomer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectCustomerActionPerformed(evt);
            }
        });

        cusAddress.setText(" ");

        cusZipcode.setText(" ");

        cusName.setFont(new java.awt.Font("Lucida Grande", 1, 13));
        cusName.setText("<Ingen kunde valgt>");

        cusPhone.setText(" ");

        cusCity.setText(" ");

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(cusName)
                    .add(cusAddress)
                    .add(jPanel3Layout.createSequentialGroup()
                        .add(cusZipcode)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(cusCity))
                    .add(selectCustomer)
                    .add(cusPhone))
                .addContainerGap(67, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .add(cusName)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(cusAddress)
                .add(8, 8, 8)
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(cusZipcode)
                    .add(cusCity))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(cusPhone)
                .add(9, 9, 9)
                .add(selectCustomer))
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Ordre overblik"));

        jLabel10.setFont(new java.awt.Font("Lucida Grande", 1, 13));
        jLabel10.setText("Subtotal:");

        jLabel11.setText("Moms (25%):");

        jLabel12.setFont(new java.awt.Font("Lucida Grande", 1, 13));
        jLabel12.setText("Total:");

        moms.setText("0");

        jLabel17.setText("kr.");

        subTotal.setFont(new java.awt.Font("Lucida Grande", 1, 13));
        subTotal.setText("0");

        jLabel19.setFont(new java.awt.Font("Lucida Grande", 1, 13));
        jLabel19.setText("kr.");

        jLabel23.setFont(new java.awt.Font("Lucida Grande", 1, 13));
        jLabel23.setText("kr.");

        total.setFont(new java.awt.Font("Lucida Grande", 1, 13));
        total.setText("0");

        jLabel2.setText("Antal varer:");

        amountLabel.setText("0");

        jLabel5.setText("stk.");

        org.jdesktop.layout.GroupLayout jPanel4Layout = new org.jdesktop.layout.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel4Layout.createSequentialGroup()
                        .add(jLabel2)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(amountLabel)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel5))
                    .add(jPanel4Layout.createSequentialGroup()
                        .add(jLabel10)
                        .add(29, 29, 29)
                        .add(subTotal)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel19))
                    .add(jPanel4Layout.createSequentialGroup()
                        .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel11)
                            .add(jLabel12))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jPanel4Layout.createSequentialGroup()
                                .add(total)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jLabel23))
                            .add(jPanel4Layout.createSequentialGroup()
                                .add(moms)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jLabel17)))))
                .addContainerGap(102, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel4Layout.createSequentialGroup()
                .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(amountLabel)
                    .add(jLabel5))
                .add(18, 18, 18)
                .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel10)
                    .add(subTotal)
                    .add(jLabel19))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel11)
                    .add(moms)
                    .add(jLabel17))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel12)
                    .add(total)
                    .add(jLabel23))
                .addContainerGap(28, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(createLabel)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(editLabel)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jLabel4))
                            .add(layout.createSequentialGroup()
                                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 524, Short.MAX_VALUE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(plusCount)
                                    .add(minusCount)
                                    .add(removeItem))))
                        .addContainerGap())
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                                .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(18, 18, 18)
                                .add(jPanel4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(saveBtn))
                        .add(104, 104, 104))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(createLabel)
                    .add(editLabel)
                    .add(jLabel4))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(plusCount)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(minusCount)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(removeItem))
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 151, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel4, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .add(33, 33, 33)
                .add(saveBtn)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void plusCountActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_plusCountActionPerformed
        int selectedIndex = itemsTable.getSelectedRow();
        if(selectedIndex >= 0) {
            OrderItem oi = items.get(selectedIndex);
            if(oi != null) {
                orderCtr.addProduct(oi.getProduct());
            }
            updateDetails();
        }
    }//GEN-LAST:event_plusCountActionPerformed

    private void minusCountActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_minusCountActionPerformed
        int selectedIndex = itemsTable.getSelectedRow();
        if(selectedIndex >= 0) {
            OrderItem oi = items.get(selectedIndex);
            if(oi != null) {
                orderCtr.minusQuantity(oi.getProduct());
            }
            updateDetails();
        }
    }//GEN-LAST:event_minusCountActionPerformed

    private void removeItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeItemActionPerformed
        int selectedIndex = itemsTable.getSelectedRow();
        if(selectedIndex >= 0) {
            OrderItem oi = items.get(selectedIndex);
            if(oi != null) {
                orderCtr.removeProduct(oi.getProduct());
            }
            updateDetails();
        }
    }//GEN-LAST:event_removeItemActionPerformed

    private void searchFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_searchFieldKeyReleased
        if(evt.getKeyCode() == 10) {
            this.doSearch();
        }
    }//GEN-LAST:event_searchFieldKeyReleased

    private void addItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addItemActionPerformed
        int selected = productList.getSelectedIndex();
        if(selected >= 0) {
            Product product = searchProducts.get(selected);
            if(product != null) {
                orderCtr.addProduct(product.getId());
            }
        }
        updateDetails();
    }//GEN-LAST:event_addItemActionPerformed

    private void selectCustomerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectCustomerActionPerformed
        new CustomerSelectView(this).setVisible(true);
    }//GEN-LAST:event_selectCustomerActionPerformed

    private void searchBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchBtnActionPerformed
        this.doSearch();
    }//GEN-LAST:event_searchBtnActionPerformed

    private void saveBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveBtnActionPerformed
        if(orderCtr.getOrder() != null)
        {
            if(orderCtr.getCustomer() != null)
            {
                if(orderCtr.getOrderItems().size() > 0)
                {
                    if(orderCtr.getEmployee() != null)
                    {
                        if(orderIsBeingEdited)
                        {
                            if(showConfirm("Er du sikker på at du vil opdatere ordren?"))
                            {
                                if(orderCtr.updateOrder())
                                {
                                    this.showMessage("Ordren er blevet opdateret!");
                                } else {
                                    this.showError("Noget gik galt. Ordren blev ikke opdateret!");
                                }
                            }
                        } else {
                            if(showConfirm("Er du sikker på at du vil oprette ordren?"))
                            {
                                int orderId = orderCtr.createOrder();
                                if(orderId > 0)
                                {
                                    this.showMessage("Ordren er oprettet og har fået ordrenummeret: #" + orderId);
                                    dispose();
                                } else {
                                    this.showError("Der skete en fejl. Ordren blev ikke oprettet!");
                                }
                            }
                        }
                    } else {
                        this.showError("Sært, der er ikke sat nogen medarbejder. Ikke oprettet!");
                    }
                } else {
                    this.showError("Der skal være valgt mindst 1 produkt til ordren!");
                }
            } else {
                this.showError("Ingen kunde valgt!");
            }
        } else {
            this.showError("Der er sket en fejl. Ordren blev ikke oprettet!");
        }
    }//GEN-LAST:event_saveBtnActionPerformed

    public boolean setCustomer(Customer customer) {
        boolean result = false;
        if(customer != null) {
            orderCtr.setCustomer(customer);
            insertCustomerInformations();
            result = true;
        }

        return result;
    }

    private void insertCustomerInformations() {
        Customer customer = orderCtr.getCustomer();
        
        if(customer != null) {
            this.cusName.setText(customer.getName());
            this.cusAddress.setText(customer.getAddress());
            this.cusZipcode.setText("" + customer.getZipcode());
            this.cusCity.setText(customer.getCity());
            this.cusPhone.setText("Tlf. " + customer.getPhoneno());
        }
    }

    private void doSearch() {
        String search = this.searchField.getText();
        if(search.length() > 0) {
            searchProducts = this.orderCtr.searchProducts(search);
        }
        this.updateSearchList();
    }

    private void updateSearchList() {
        DefaultListModel listModel = new DefaultListModel();

        if(searchProducts.size() > 0) {
            if(searchProducts != null) {
                for(Product p : searchProducts) {

                    listModel.addElement(p.getName() + " (#" + p.getId() + ") - " + p.getSalesPrice() + " kr.");
                }
            }
        }

        productList.removeAll();
        productList.setModel(listModel);
    }

    private void updateDetails() {
        items = orderCtr.getOrderItems();
        Order order = orderCtr.getOrder();

        Object quantity = "Antal";
        Object title = "Titel";
        Object price = "Á";
        Object subtotal = "Subtotal";

        Object obj[] = null;

        DefaultTableModel listModel = new DefaultTableModel();

        listModel.addColumn(quantity);
        listModel.addColumn(title);
        listModel.addColumn(price);
        listModel.addColumn(subtotal);

        if(items.size() > 0) {
            for(OrderItem oi : items) {
                Product item = oi.getProduct();

                obj = new Object [] { oi.getQuantity(), item.getName(), item.getSalesPrice(), oi.getSubTotal() };
                listModel.addRow(obj);
            }
        }

        itemsTable.removeAll();
        itemsTable.setModel(listModel);

        double p = order.getTotalPrice();
        double vat = p*0.25;

        amountLabel.setText("" + order.getAmount());

        subTotal.setText("" + p);
        moms.setText("" + vat);
        total.setText("" + (p + vat));
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addItem;
    private javax.swing.JLabel amountLabel;
    private javax.swing.JLabel createLabel;
    private javax.swing.JLabel cusAddress;
    private javax.swing.JLabel cusCity;
    private javax.swing.JLabel cusName;
    private javax.swing.JLabel cusPhone;
    private javax.swing.JLabel cusZipcode;
    private javax.swing.JLabel editLabel;
    private javax.swing.JTable itemsTable;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton minusCount;
    private javax.swing.JLabel moms;
    private javax.swing.JButton plusCount;
    private javax.swing.JList productList;
    private javax.swing.JButton removeItem;
    private javax.swing.JButton saveBtn;
    private javax.swing.JButton searchBtn;
    private javax.swing.JTextField searchField;
    private javax.swing.JButton selectCustomer;
    private javax.swing.JLabel subTotal;
    private javax.swing.JLabel total;
    // End of variables declaration//GEN-END:variables

}
